<template>
  <d2-container>
    <!-- <p v-for="item in list">
      {{item.username}}
    </p> -->
    <!-- <table>
      <tr>
        <th>id</th>
        <th>用户名</th>
        <th>密码</th>
        <th>最后登录时间</th>
      </tr>
      <tr v-for="item in list">
        <th>{{item.id}}</th>
        <th>{{item.username}}</th>
        <th>{{item.password}}</th>
        <th>{{item.lastLogin}}</th>
      </tr>
    </table> -->
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item
        v-for="(item, index) in breadcrumbList"
        :key="index"
        :to="{ path: item.path }">
        {{item.title}}
      </el-breadcrumb-item>
    </el-breadcrumb> -->
    <br>
    <el-card class="box-card">
      <el-form>
        <el-form-item>
          <el-button type="success" icon="el-icon-plus" @click="addUserDialog">添加用户</el-button>
        </el-form-item>
      </el-form>
    <el-table
        :data="list"
        border
        stripe
        max-height="400"
        style="width: 100%;">
        <el-table-column
          prop="id"
          label="id"
          width="120">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="100">
        </el-table-column>
        <el-table-column
          sortable
          prop="password"
          label="密码"
          width="80">
        </el-table-column>
        <el-table-column
          sortable
          prop="lastLogin"
          label="最后登录时间"
          width="130">
        </el-table-column>

        <el-table-column
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="del(scope.row.id)"></el-button>
            <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="padding-top: 15px"
        @size-change="handlePageSizeChange"
        @current-change="handlePageNumberChange"
        :current-page="pageNumber"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal">
      </el-pagination>
      <br>
      <el-row>
        <el-tag>{{msg}}</el-tag>
        <el-button type="success" round @click="refreshMsg()">刷新</el-button>
      </el-row>

    </el-card>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @open="onOpen" @close="onClose">
  <el-form ref="addUserForm" :model="addUserFormData" :rules="addUserRules" size="medium" label-width="100px">
    <el-row>
      <el-col :span="16">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addUserFormData.username" placeholder="请输入用户名" clearable
            prefix-icon='el-icon-user' :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="16">
        <el-form-item label="密码" prop="password">
          <el-input v-model="addUserFormData.password" placeholder="请输入密码" clearable prefix-icon='el-icon-key'
            show-password :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <div slot="footer">
    <el-button @click="dialogFormVisible = false">取消</el-button>
    <el-button type="primary" @click="handelConfirm">确定</el-button>
  </div>
</el-dialog>
  </d2-container>

</template>

<script>
import api from '@/api'
export default {
  name: 'usertable',
  data () {
    return {
      list: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: null,
      pageSizes: [6, 10, 20, 30],
      msg: '',
      breadcrumbList: [{
        title: '首页',
        path: '/index'
      }],
      dialogFormVisible: false,
      addUserFormData: {
        userid: undefined,
        username: undefined,
        password: undefined
      },
      addUserRules: {
        username: [{
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }]
      },
      title: '',
      action: ''
    }
  },
  computed: {},
  watch: {

  },
  mounted () {},
  created () {
    this.findUserListData()
    console.log('----------路由--------')
    console.log(this.$route)
    this.refreshMsg()
    // this.getRouterList()
  },
  methods: {
    async findUserListData (pageNumber, pageSize) {
      pageNumber = this.pageNumber
      pageSize = this.pageSize
      const res = await api.FIND_USER_LIST({ pageNumber, pageSize })
      this.list = res.userlist.list
      this.pageTotal = res.userlist.total
    },
    handlePageSizeChange (val) {
      this.pageSize = val
      this.findUserListData()
    },
    handlePageNumberChange (val) {
      this.pageNumber = val
      this.findUserListData()
    },
    async refreshMsg () {
      const res = await api.REFRESH_MSG()
      this.msg = res.msg
    },
    // async getRouterList () {
    //   this.breadcrumbList.push({ title: this.$route.meta.title, path: this.$route.path })
    // },
    async addUser () {
      const res = await api.ADD_USER({
        username: this.addUserFormData.username,
        password: this.addUserFormData.password
      })
      if (res.msg === '添加成功') {
        this.$message({
          message: res.msg,
          type: 'success'
        })
      }
    },
    async updateUser (data = {}) {
      const res = await api.UPDATE_USER({
        id: this.addUserFormData.userid,
        username: this.addUserFormData.username,
        password: this.addUserFormData.password
      })
      if (res.msg === '修改成功') {
        this.$message({
          message: res.msg,
          type: 'success'
        })
      }
    },
    addUserDialog () {
      this.dialogFormVisible = true
      this.title = '添加用户'
      this.action = 'add'
    },
    edit (data) {
      this.action = 'edit'
      this.dialogFormVisible = true
      this.title = '编辑用户'
      this.addUserFormData.userid = data.id
      this.addUserFormData.username = data.username
      this.addUserFormData.password = data.password
    },
    onOpen () {
      // 打开窗口后执行的方法

    },
    onClose () {
      // 关闭弹窗后执行的方法
      this.$refs.addUserForm.resetFields()
      this.action = ''
    },
    handelConfirm () {
      if (this.action === 'add') {
        this.$refs.addUserForm.validate(valid => {
          if (valid) {
            this.addUser()
            this.dialogFormVisible = false
            this.findUserListData()
          } else {
            this.$message({
              message: '必填项为空',
              type: 'warning'
            })
          }
        })
      } else if (this.action === 'edit') {
        this.$refs.addUserForm.validate(valid => {
          if (valid) {
            this.updateUser()
            this.dialogFormVisible = false
            this.findUserListData()
          } else {
            this.$message({
              message: '必填项为空',
              type: 'warning'
            })
          }
        })
      }
    },
    async del (data) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const res = api.DELETE_USER_BY_ID({ id: data })
        if (res) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      }).finally(() => {
        this.findUserListData()
      })
    }
  }
}
</script>
